package org.example.proxy;

import org.example.RpcApplication;

import java.lang.reflect.Proxy;

/**
 * mock代理工厂
 */
public class MockServiceProxyFactory {
    /**
     * 获取代理对象根据服务类型
     * @param clazz
     * @return
     * @param <T>
     */
    public static <T> T getProxy(Class<T> clazz){
        if (RpcApplication.getRpcConfig().isMock()){
            return getMockProxy(clazz);
        }

        return(T) Proxy.newProxyInstance(clazz.getClassLoader(), new Class[]{clazz}, new ServiceProxy());
    }

    /**
     * 获取mock代理对象
     * @param clazz
     * @return
     * @param <T>
     */

    public static <T> T getMockProxy(Class<T> clazz){
        return (T) Proxy.newProxyInstance(clazz.getClassLoader(), new Class[]{clazz}, new MockServiceProxy());
    }

}
